import meetingRoom from '../../../config/api/meetingRoom' // 引入api接口管理文件
var WxNotificationCenter = require("../../../config/WxNotificationCenter");
import {
  baseUrl
} from '../../../config/config';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    id: null,
    name: "",
    place: "",
    room: "",
    imgUrl: "",
  },
  clicktijiao() {
    if (this.data.name.length == 0) {
      wx.showToast({
        title: '请输入会议室名称',
        icon: "none",
      })
      return;
    }
    if (this.data.name.place == 0) {
      wx.showToast({
        title: '请输入地点',
        icon: "none",
      })
      return;
    }
    if (this.data.name.room == 0) {
      wx.showToast({
        title: '请输入房间',
        icon: "none",
      })
      return;
    }
    //分割字符串

    wx.showLoading({
      title: '加载中...',
      mask: true,
    })
    if (this.data.id) {
      meetingRoom.meetingRoomupdateById({
        "id": this.data.id,
        "name": this.data.name,
        "place": this.data.place,
        "room": this.data.room,
        "imgUrl": this.data.imgUrl
      }, function (res) {
        wx.hideLoading({
          success: (res) => {},
        })
        if (res.data.status == 200) {
          wx.showModal({
            title: '修改成功',
            showCancel: false,
            success: (result) => {},
            fail: (res) => {},
            complete: (res) => {
              wx.navigateBack({
                delta: 1,
              })
            },
          })
          WxNotificationCenter.postNotificationName("addroom");
        }
      })
    } else {
      meetingRoom.meetingRoomsave({
        "name": this.data.name,
        "place": this.data.place,
        "room": this.data.room,
        "imgUrl": this.data.imgUrl
      }, function (res) {
        wx.hideLoading({
          success: (res) => {},
        })
        if (res.data.status == 200) {
          wx.showModal({
            title: '添加成功',
            showCancel: false,
            success: (result) => {},
            fail: (res) => {},
            complete: (res) => {
              wx.navigateBack({
                delta: 1,
              })
            },
          })
          WxNotificationCenter.postNotificationName("addroom");
        }
      })
    }

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    if (options["item"]) {
      let item = JSON.parse(options.item);
      console.log(item);
      this.setData({
        id: item.id,
        name: item.name,
        place: item.place,
        room: item.room,
        imgUrl: item.imgUrl

      })
      wx.setNavigationBarTitle({
        title: '编辑会议室',
      })
    }
  },
  // 上传文件
  clickupfile() {
    let vm = this;
    wx.chooseMedia({
      count: 1,
      mediaType: ['image'],
      sourceType: ['album', 'camera'],
      success(res) {
        // tempFilePath可以作为 img 标签的 src 属性显示图片
        const tempFilePaths = res.tempFiles[0].tempFilePath;
        let url = baseUrl + 'aliyunFile/uploadFile';
        wx.showLoading({
          title: '上传中...',
        })
        wx.uploadFile({
          url: url, //仅为示例，非真实的接口地址
          filePath: tempFilePaths,
          name: 'file',
          success(res) {

            const data = res.data
            let urlpath = JSON.parse(data)["data"];
            vm.setData({
              imgUrl: urlpath
            })
          },
          fail(error) {
            console.log(error);
          },
          complete() {
            console.log("complete");
            wx.hideLoading({
              success: (res) => {},
            })
          }
        })
      }
    })
  },
  clickdel(e) {
    let content = `确定要删除么?`
    let vm = this;
    wx.showModal({
      title: '提示',
      content: content,
      success(res) {
        if (res.confirm) {
          //开始删除
          wx.showLoading({
            title: '删除中...',
          })
          meetingRoom.meetingRoomdel({
            "id": vm.data.id
          }, function (res) {
            wx.hideLoading({
              success: (res) => {},
            })
            if (res.data.status == 200) {
              wx.showModal({
                title: '删除成功',
                showCancel: false,
                success: (result) => {},
                fail: (res) => {},
                complete: (res) => {
                  wx.navigateBack({
                    delta: 1,
                  })
                },
              })
              WxNotificationCenter.postNotificationName("addroom");
            } else {
              wx.showToast({
                title: res.data.msg,
                icon: "none"
              })
            }
          })
        } else if (res.cancel) {

        }
      }
    })
  },
  //处理文本双向绑定问题
  onChange(e) {
    console.log(e.detail.value) //input 的 value
    console.log(e.currentTarget.dataset.prop) //data-prop 绑定的字符串，以此来确定改变的是哪个变量
    this.setData({
      [e.currentTarget.dataset.prop]: e.detail.value
    })
  },
  //处理带数组类型的绑定
  moretypechange(e) {
    let arr = this.data[e.currentTarget.dataset.arrdata];
    this.setData({
      [e.currentTarget.dataset.prop]: e.detail.value
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})